<?php
namespace app\common\model;

use think\facade\Log;

class Room extends Common
{
    protected $autoWriteTimestamp = true;
    protected $createTime = 'ctime';

    /**
     * 获取直播用户基本信息
     */
    public function userRoom($data){
        $room=$this->alias('a')
            ->where('u.id',$data['uid'])
            ->join('zp_user u','a.uid=u.id','right')
            ->field('a.*,u.nickname,u.avatar,u.sex,u.sign')
            ->find();
        $attention=0;
        $follow=LiveFollow::where(['f_uid'=>$data['uid'],'uid'=>$data['user_id']])->find();
        if($follow){
            $attention=$follow['status'];
        }
        #自己的直播
        if($data['uid']==$data['user_id']){
            $attention=2;
        }
        $room['attention']=$attention;

        return $room;
    }
    /**
     * 直播房间数据
     */
    public function roomInfo($data){
        $room=$this->where('uid',$data['uid'])->find();
        return $room;
    }

    /**
     * 播放视频
     */
    public function liveView($data){
        $room=$this->alias('a')
                ->where('a.id',$data['id'])
                ->join('zp_user u','a.uid=u.id')
                ->field('a.*,u.nickname,u.avatar')
                ->find();
        $maps=[
            ['room_id','=',$data['id']],
            ['times','>',0],
            ['utime','>',0]
        ];
        if($room->is_live==1){
            $maps=[
                ['room_id','=',$data['id']],
                ['times','=',0],
                ['utime','=',0]
            ];
        }
        $live=Live::where($maps)->order('id desc')->find();
        $attention=0;
        $follow=LiveFollow::where(['f_uid'=>$room['uid'],'uid'=>$data['uid']])->find();
        if($follow){
            $attention=$follow['status'];
        }
        if($room['uid']==$data['uid']){#自己的直播
            $attention=2;
        }else{
            //写入进入房间事件
            $view=new LiveView();
            list($res,$msg)=$view->onView(['room_id'=>$room['id'],'live_id'=>$live['id'],'uid'=>$data['uid']]);
            if(!$res){
                Log::error($msg);
            }
        }
        $room['live']=$live;
        $room['attention']=$attention;
        $room['live_play_url']=getSetting('live_vod_domain');#点播
        $room['live_live_url']=getSetting('live_play_domain');#直播
        $room['userlist']=LiveView::alias('a')
            ->join('zp_user u','a.uid=u.id')
            ->where(['a.room_id'=>$room['id'],'a.live_id'=>$live['id'],'a.is_line'=>1])
            ->field('u.id,u.nickname,u.avatar')->limit(6)->order('id desc')->select();
        $room['user']=User::where('id',$data['uid'])->field('id,nickname,avatar')->find();
        return $room;
    }

    /**
     * 直播视频列表
     */
    public function liveList($data){
        $list=$this->alias('a')
            ->join('zp_user u','a.uid=u.id')
            ->field('a.*,u.nickname,u.avatar')
            ->where('u.status',1);
        $list=$list->page($data['page'],$data['limit'])->order('is_live desc,id desc')->select();
        foreach($list as $k=>$v){
            $list[$k]['live']=Live::where('room_id',$v['id'])->order('id desc')->find();
            if(stripos($v['cover'],'_thumb')){
                $list[$k]['thumb']=$v['cover'];
            }else{
                $list[$k]['thumb']=$v['cover'].'_thumb';
            }
        }
        $total=$list->count();
        return ['count'=>$total,'list'=>$list];
    }
    /**
     * 创建直播房间
     * @param array $data 数据
     *
     */
    public function createRoom($data)
    {
        $room=$this->where('uid',$data['uid'])->find();
        if($room){
            $room->city_id=$data['city_id'];
            $room->is_live=$data['is_live'];
            $room->sort_id=$data['sort_id'];
            $room->cover=$data['cover'];
            $room->utime=$data['utime'];
            $room->save();
        }else{
            $room=$this->create($data,true);
        }
        return [true,$room->id];
    }

    public function changeAvatar($id, $image_url)
    {
        $data['avatar'] = $image_url;
        $where['id']    = $id;
        if ($this->save($data, $where)) {
            return true;
        } else {
            return false;
        }
    }
    /**
     * 用户信息表关联
     * @return [type] [description]
     */
    public function user(){
        return $this->hasOne(User::class,'id','uid');
    }
    /**
     * 商品分类表关联
     * @return [type] [description]
     */
    public function goodcat(){
        return $this->hasOne(GoodsCat::class,'id','sort_id');
    }
    /**
     * 区域表关联
     * @return [type] [description]
     */
    public function area(){
        return $this->hasOne(Area::class,'id','city_id');
    }
    /**
     * 时间范围查询
     * @param  [type] $query [description]
     * @param  [type] $value [description]
     * @param  [type] $data  [description]
     * @return [type]        [description]
     */
    public function searchCtimeAttr($query,$value, $data)
    {
        $start=$value[0]?strtotime($value[0]):0;
        $end=$value[1]?strtotime($value[1]):0;
        if(!empty($start)&&!empty($end)){
            $query->where('room.ctime','between',[$start,$end]);
        }elseif(!empty($start)&&empty($end)){
            $query->where('room.ctime','>',$start);
        }elseif(empty($start)&&!empty($end)){
            $query->where('room.ctime','<',$end);
        }
    }
    /**
     * 关键字查询
     * @param  [type] $query [description]
     * @param  [type] $value [description]
     * @param  [type] $data  [description]
     * @return [type]        [description]
     */
    public function searchNameAttr($query,$value, $data)
    {
        if(!empty($value)){
            $query->where('room.uid',$value);
        }
    }
}
